import os
from client.pages.GroupEditWin import *
from PyQt5.QtWidgets import QMainWindow, QFileDialog, QListWidgetItem, QMessageBox
from PyQt5.QtCore import Qt
from client.func.ClientHttp import get_group_info_post, change_group_info_post, out_group_post
from client.func.ImgOperate import compress_image


root = "./"
img_dir = "client/.cache/img_buf/"


class GroupEditPage(QMainWindow, Ui_GroupEditWin):
    def __init__(self):
        super(GroupEditPage, self).__init__()
        self.setupUi(self)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.myuid = ""
        self.uid = ""
        self.explain = ""
        self.name = u"昵称"
        self.img_path = root + img_dir + "group_default.png"
        self.search_accept = True
        self.master = ""
        self.members = []

        self.name_edit_button.clicked.connect(self.OnEditName)
        self.name_edit.editingFinished.connect(self.OnEditNameFinished)
        self.change_img_button.clicked.connect(self.OnChangeImg)
        self.save_button.clicked.connect(self.OnSaveChange)
        self.close_button.clicked.connect(self.OnClose)

        self.invite_button.hide()  # 邀请人加群的功能，不想写了
        # self.member_lineEdit.setReadOnly(True)
        self.memberWidget.itemClicked.connect(self.OnChooseMember)
        self.delete_button.clicked.connect(self.OnDeleteMember)

    def OnChooseMember(self, item):
        self.member_lineEdit.setText(item.text())

    def OnDeleteMember(self):
        uid = self.member_lineEdit.text()
        if uid == self.master:
            QMessageBox.information(self, "这是群主", "不能踢群主", QMessageBox.Ok)
        elif uid in self.members:
            res = QMessageBox.question(self, "确认踢人", "是否确认将此人踢出群?", QMessageBox.Ok|QMessageBox.No)
            if res == QMessageBox.Ok:
                out_group_post(uid, self.uid)
                self.update()
        else:
            QMessageBox.information(self, "查无此人", "查无此人", QMessageBox.Ok)


    def update(self):
        info = get_group_info_post(self.uid)
        if info:
            self.name = info["name"]
            self.img_path = root + img_dir + info["img"]
            self.explain = info["explain"]
            self.search_accept = info["search_accept"]
            self.master = info["master"]
            self.members = info["members"]
            self.set_img()
            self.name_edit.setText(self.name)
            self.uid_label.setText("uid: " + self.uid)
            self.explainEdit.setText(self.explain)
            self.can_search.setChecked(self.search_accept)
            self.memberWidget.clear()
            self.memberWidget.addItems(self.members)

    def OnSaveChange(self):
        info = {
            "name": self.name,
            "img_path": self.img_path,
            "search_accept": self.can_search.isChecked(),
            "explain": self.explainEdit.toPlainText()
        }
        status = change_group_info_post(self.uid, info)

    def OnChangeImg(self):
        file_path, file_type = QFileDialog.getOpenFileName(self, "选择图片", "./", "All Files (*);;jpg(*.jpg);;png(*.png)")
        file_type = os.path.basename(file_path).split(".")[-1]
        if file_type in ["jpg", "png", "jpeg"]:
            self.img_path = compress_image(file_path)  # 压缩图片
            self.set_img()

    def OnEditName(self):
        self.name_edit.setReadOnly(False)
        self.name_edit.selectAll()
        self.name_edit.setFocus()

    def OnEditNameFinished(self):
        self.name_edit.setReadOnly(True)
        if self.name_edit.text():
            self.name = self.name_edit.text()
        else:
            self.name_edit.setText(self.name)

    def set_img(self):
        if os.path.exists(self.img_path):
            img = QtGui.QPixmap(self.img_path).scaled(self.img_label.width(), self.img_label.height())
            self.img_label.setPixmap(img)

    def OnClose(self):
        self.close()
